home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.05 May 90 / Language Translation Code / Errors / Dec.c next >
Encoding:
C/C++ Source or Header  |  1989-11-30  |  10.6 KB  |  433 lines  |  [TEXT/MPS ]

  1. #define ILLEGAL 257
  2. #define NUM 258
  3. #define MINUS 259
  4. #ifndef YYSTYPE
  5. #define YYSTYPE int
  6. #endif
  7. YYSTYPE yylval, yyval;
  8. #define YYERRCODE 256
  9.  
  10. # line 60 "Dec.y"
  11.  
  12.  
  13. /****************************************************************/
  14.  
  15. #include "stdio.h"
  16. #include "ctype.h"
  17. #include "string.h"
  18.     
  19. /****************************************************************/
  20.  
  21. char                *input;
  22. char                *token;
  23.     
  24. /****************************************************************/
  25.  
  26. #define yyerror(x) {errordisplay(tmpstate, pcyytoken); return(0);}
  27.  
  28. yyparse();
  29. int yylex();
  30. void errordisplay(int state, int tokentype);
  31.     
  32. /****************************************************************/
  33.  
  34. void main(int argc, char *argv[])
  35.     {
  36.     
  37.     char                thestring[256];
  38.     
  39.     if (argc < 1)
  40.         printf("\tMajor serious real bad impossible error!\n");
  41.     else if (argc > 2)
  42.         printf("\tHey!  One at a time!\n");
  43.     else if (argc == 2)
  44.         {
  45.         input = argv[1];
  46.         yyparse();
  47.         }
  48.     else
  49.         {
  50.         printf("? ");
  51.         while (strlen(gets(thestring)) > 2)
  52.             {
  53.             input = &thestring[2];
  54.             yyparse();
  55.             printf("? ");
  56.             }
  57.         }
  58.     
  59.     }
  60.     
  61. /****************************************************************/
  62.     
  63. int yylex()
  64.     {
  65.     
  66.     token = strtok(input, " ");
  67.     input = 0;
  68.     
  69.     if (token == 0)
  70.         return('\n');
  71.     else if (sscanf(token, "%x", &yylval) == 1)
  72.         return(NUM);
  73.     else switch(token[0])
  74.         {
  75.         case '\n':
  76.         case '(':
  77.         case ')':
  78.         case '+':
  79.         case '-':
  80.             return(token[0]);
  81.         default:
  82.             return(ILLEGAL);
  83.         }
  84.     
  85.     }
  86.     
  87. /****************************************************************/
  88.     
  89. void errordisplay(int state, int tokentype)
  90.     {
  91.     
  92.     if (tokentype == ILLEGAL)
  93.         printf("\tIllegal character!\n");
  94.     else switch (state)
  95.         {
  96.         
  97.         case 0:
  98.         case 3:
  99.         case 4:
  100.         case 7:
  101.         case 8:
  102.         case 9:
  103.         case 10:
  104.             printf("\tExpecting a number, ‘-’ or ‘(’!\n");
  105.             break;
  106.         
  107.         case 2:
  108.             printf("\tExpecting an operator or end of input!\n");
  109.             break;
  110.         
  111.         case 12:
  112.             printf("\tExpecting an operator or ‘)’!\n");
  113.             break;
  114.         
  115.         case 13:
  116.         case 14:
  117.             printf("\tExpecting a ‘*’ or ‘/’!\n");
  118.             break;
  119.         
  120.         default:
  121.             printf("\tImpossible error!  State = %d, token = %d\n",
  122.                             state, tokentype);
  123.             break;
  124.         
  125.         }
  126.     
  127.     }
  128.     
  129. /****************************************************************/
  130.  
  131.  
  132. #include <stdio.h>
  133. FILE *yytfilep;
  134. char *yytfilen;
  135. int yytflag = 0;
  136. int svdprd[2];
  137. char svdnams[2][2];
  138.  
  139. int *yyxi;
  140. int yyexca[] = {
  141.   -1, 1,
  142.   0, -1,
  143.   -2, 0,
  144.   0,
  145. };
  146.  
  147. #define YYNPROD 9
  148. #define YYLAST 219
  149.  
  150. int yyact[] = {
  151.        4,       6,      17,       9,       7,       3,       8,       9,
  152.       10,       2,       1,       0,      10,      11,      12,       0,
  153.        0,      13,      14,      15,      16,       4,       7,       8,
  154.        9,      10,       2,       0,       0,       0,       9,       7,
  155.        0,       8,       0,      10,       0,       0,       0,       0,
  156.        0,       0,       0,       0,       0,       0,       0,       0,
  157.        0,       0,       0,       0,       0,       0,       0,       0,
  158.        0,       0,       0,       0,       0,       0,       0,       0,
  159.        0,       0,       0,       0,       0,       0,       0,       0,
  160.        0,       0,       0,       0,       0,       0,       0,       0,
  161.        0,       0,       0,       0,       0,       0,       0,       0,
  162.        0,       0,       0,       0,       0,       0,       0,       0,
  163.        0,       0,       0,       0,       0,       0,       0,       0,
  164.        0,       0,       0,       0,       0,       0,       0,       0,
  165.        0,       0,       0,       0,       0,       0,       0,       0,
  166.        0,       0,       0,       0,       0,       0,       0,       0,
  167.        0,       0,       0,       0,       0,       0,       0,       0,
  168.        0,       0,       0,       0,       0,       0,       0,       0,
  169.        0,       0,       0,       0,       0,       0,       0,       0,
  170.        0,       0,       0,       0,       0,       0,       0,       0,
  171.        0,       0,       0,       0,       0,       0,       0,       0,
  172.        0,       0,       0,       0,       0,       0,       0,       0,
  173.        0,       0,       0,       0,       0,       0,       0,       0,
  174.        0,       0,       0,       0,       0,       0,       0,       0,
  175.        0,       0,       0,       0,       0,       0,       0,       0,
  176.        0,       0,       0,       0,       0,       0,       0,       0,
  177.        0,       0,       0,       0,       0,       0,       0,       0,
  178.        0,       0,       5,
  179. };
  180.  
  181. int yypact[] = {
  182.      -40,   -1000,     -12,     -40,     -40,   -1000,   -1000,     -40,
  183.      -40,     -40,     -40,   -1000,     -39,     -35,     -35,   -1000,
  184.    -1000,   -1000,
  185. };
  186.  
  187. int yypgo[] = {
  188.        0,      10,       9,
  189. };
  190.  
  191. int yyr1[] = {
  192.        0,       1,       2,       2,       2,       2,       2,       2,
  193.        2,
  194. };
  195.  
  196. int yyr2[] = {
  197.        2,       2,       3,       3,       3,       3,       2,       3,
  198.        1,
  199. };
  200.  
  201. int yychk[] = {
  202.    -1000,      -1,      -2,      45,      40,     258,      13,      43,
  203.       45,      42,      47,      -2,      -2,      -2,      -2,      -2,
  204.       -2,      41,
  205. };
  206.  
  207. int yydef[] = {
  208.        0,      -2,       0,       0,       0,       8,       1,       0,
  209.        0,       0,       0,       6,       0,       2,       3,       4,
  210.        5,       7,
  211. };
  212.  
  213. /*****************************************************************/
  214. /* PCYACC LALR parser driver routine -- a table driven procedure */
  215. /* for recognizing sentences of a language defined by the        */
  216. /* grammar that PCYACC analyzes. An LALR parsing table is then   */
  217. /* constructed for the grammar and the skeletal parser uses the  */
  218. /* table when performing syntactical analysis on input source    */
  219. /* programs. The actions associated with grammar rules are       */
  220. /* inserted into a switch statement for execution.               */
  221. /*****************************************************************/
  222.  
  223.  
  224. #ifndef YYMAXDEPTH
  225. #define YYMAXDEPTH 200
  226. #endif
  227. #ifndef YYREDMAX
  228. #define YYREDMAX 1000
  229. #endif
  230. #define PCYYFLAG -1000
  231. #define WAS0ERR 0
  232. #define WAS1ERR 1
  233. #define WAS2ERR 2
  234. #define WAS3ERR 3
  235. #define yyclearin pcyytoken = -1
  236. #define yyerrok   pcyyerrfl = 0
  237. YYSTYPE yyv[YYMAXDEPTH];     /* value stack */
  238. int pcyyerrct = 0;           /* error count */
  239. int pcyyerrfl = 0;           /* error flag */
  240. int redseq[YYREDMAX];
  241. int redcnt = 0;
  242. int pcyytoken = -1;          /* input token */
  243.  
  244.  
  245. yyparse()
  246. {
  247.   int statestack[YYMAXDEPTH]; /* state stack */
  248.   int      j, m;              /* working index */
  249.   YYSTYPE *yypvt;
  250.   int      tmpstate, tmptoken, *yyps, n;
  251.   YYSTYPE *yypv;
  252.  
  253.  
  254.   tmpstate = 0;
  255.   pcyytoken = -1;
  256. #ifdef YYDEBUG
  257.   tmptoken = -1;
  258. #endif
  259.   pcyyerrct = 0;
  260.   pcyyerrfl = 0;
  261.   yyps = &statestack[-1];
  262.   yypv = &yyv[-1];
  263.  
  264.  
  265.   enstack:    /* push stack */
  266. #ifdef YYDEBUG
  267.     printf("at state %d, next token %d\n", tmpstate, tmptoken);
  268. #endif
  269.     if (++yyps - &statestack[YYMAXDEPTH] > 0) {
  270.       yyerror("pcyacc internal stack overflow");
  271.       return(1);
  272.     }
  273.     *yyps = tmpstate;
  274.     ++yypv;
  275.     *yypv = yyval;
  276.  
  277.  
  278.   newstate:
  279.     n = yypact[tmpstate];
  280.     if (n <= PCYYFLAG) goto defaultact; /*  a simple state */
  281.  
  282.  
  283.     if (pcyytoken < 0) if ((pcyytoken=yylex()) < 0) pcyytoken = 0;
  284.     if ((n += pcyytoken) < 0 || n >= YYLAST) goto defaultact;
  285.  
  286.  
  287.     if (yychk[n=yyact[n]] == pcyytoken) { /* a shift */
  288. #ifdef YYDEBUG
  289.       tmptoken  = pcyytoken;
  290. #endif
  291.       pcyytoken = -1;
  292.       yyval = yylval;
  293.       tmpstate = n;
  294.       if (pcyyerrfl > 0) --pcyyerrfl;
  295.       goto enstack;
  296.     }
  297.  
  298.  
  299.   defaultact:
  300.  
  301.  
  302.     if ((n=yydef[tmpstate]) == -2) {
  303.       if (pcyytoken < 0) if ((pcyytoken=yylex())<0) pcyytoken = 0;
  304.       for (yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=tmpstate); yyxi += 2);
  305.       while (*(yyxi+=2) >= 0) if (*yyxi == pcyytoken) break;
  306.       if ((n=yyxi[1]) < 0) { /* an accept action */
  307.         if (yytflag) {
  308.           int ti; int tj;
  309.           yytfilep = fopen(yytfilen, "w");
  310.           if (yytfilep == NULL) {
  311.             fprintf(stderr, "Can't open t file: %s\n", yytfilen);
  312.             return(0);          }
  313.           for (ti=redcnt-1; ti>=0; ti--) {
  314.             tj = svdprd[redseq[ti]];
  315.             while (strcmp(svdnams[tj], "$EOP"))
  316.               fprintf(yytfilep, "%s ", svdnams[tj++]);
  317.             fprintf(yytfilep, "\n");
  318.           }
  319.           fclose(yytfilep);
  320.         }
  321.         return (0);
  322.       }
  323.     }
  324.  
  325.  
  326.     if (n == 0) {        /* error situation */
  327.       switch (pcyyerrfl) {
  328.         case WAS0ERR:          /* an error just occurred */
  329.           yyerror("syntax error");
  330.           yyerrlab:
  331.             ++pcyyerrct;
  332.         case WAS1ERR:
  333.         case WAS2ERR:           /* try again */
  334.           pcyyerrfl = 3;
  335.     /* find a state for a legal shift action */
  336.           while (yyps >= statestack) {
  337.       n = yypact[*yyps] + YYERRCODE;
  338.       if (n >= 0 && n < YYLAST && yychk[yyact[n]] == YYERRCODE) {
  339.         tmpstate = yyact[n];  /* simulate a shift of "error" */
  340.         goto enstack;
  341.             }
  342.       n = yypact[*yyps];
  343.  
  344.  
  345.       /* the current yyps has no shift on "error", pop stack */
  346. #ifdef YYDEBUG
  347.             printf("error: pop state %d, recover state %d\n", *yyps, yyps[-1]);
  348. #endif
  349.       --yyps;
  350.       --yypv;
  351.     }
  352.  
  353.  
  354.     yyabort:
  355.       return(1);
  356.  
  357.  
  358.   case WAS3ERR:  /* clobber input char */
  359. #ifdef YYDEBUG
  360.           printf("error: discard token %d\n", pcyytoken);
  361. #endif
  362.           if (pcyytoken == 0) goto yyabort; /* quit */
  363.     pcyytoken = -1;
  364.     goto newstate;      } /* switch */
  365.     } /* if */
  366.  
  367.  
  368.     /* reduction, given a production n */
  369. #ifdef YYDEBUG
  370.     printf("reduce with rule %d\n", n);
  371. #endif
  372.     if (yytflag && redcnt<YYREDMAX) redseq[redcnt++] = n;
  373.     yyps -= yyr2[n];
  374.     yypvt = yypv;
  375.     yypv -= yyr2[n];
  376.     yyval = yypv[1];
  377.     m = n;
  378.     /* find next state from goto table */
  379.     n = yyr1[n];
  380.     j = yypgo[n] + *yyps + 1;
  381.     if (j>=YYLAST || yychk[ tmpstate = yyact[j] ] != -n) tmpstate = yyact[yypgo[n]];
  382.     switch (m) { /* actions associated with grammar rules */
  383.       
  384.       case 1:
  385. # line 10 "Dec.y"
  386.       {
  387.                   printf("\t= %d\n", yypvt[-1]);
  388.                   return(0);
  389.                   } break;
  390.       case 2:
  391. # line 18 "Dec.y"
  392.       {
  393.                   yyval = yypvt[-2] + yypvt[-0];
  394.                   } break;
  395.       case 3:
  396. # line 23 "Dec.y"
  397.       {
  398.                   yyval = yypvt[-2] - yypvt[-0];
  399.                   } break;
  400.       case 4:
  401. # line 28 "Dec.y"
  402.       {
  403.                   yyval = yypvt[-2] * yypvt[-0];
  404.                   } break;
  405.       case 5:
  406. # line 33 "Dec.y"
  407.       {
  408.                   if (yypvt[-0] != 0)
  409.                       yyval = yypvt[-2] / yypvt[-0];
  410.                   else
  411.                       {
  412.                       printf("Attempt to divide by zero!\n");
  413.                       return(0);
  414.                       }
  415.                   } break;
  416.       case 6:
  417. # line 44 "Dec.y"
  418.       {
  419.                   yyval = - yypvt[-0];
  420.                   } break;
  421.       case 7:
  422. # line 49 "Dec.y"
  423.       {
  424.                   yyval = yypvt[-1];
  425.                   } break;
  426.       case 8:
  427. # line 54 "Dec.y"
  428.       {
  429.                   yyval = yypvt[-0];
  430.                   } break;    }
  431.     goto enstack;
  432. }
  433.